import axios from './http';
import qs from 'qs';

const http = {
  async get(url, data) {
    let dataNew = data;
    let time = (new Date()).valueOf();
    if(dataNew) {
      dataNew.time = time
    }
    try {
      let res = await axios.get(url, {
        params: dataNew
      })
      res = res.data
      return new Promise((resolve, reject) => {
        if (res.code === 200) {
          resolve(res)
        } else {
          reject(res)
        }
      })
    } catch (err) {
      alert('服务器出错')
      console.log(err)
    }
  },
  async post(url, data) {
    try {
      let res = await axios.post(url, qs.stringify(data))
      // let res = await axios.post(url, data)
      res = res.data
      return new Promise((resolve, reject) => {
        if (res.code === 200) {
          resolve(res)
        } else {
          reject(res)
        }
      })
    } catch (err) {
      alert('服务器出错')
      console.log(err)
    }
  }
}

export { http }
